package com.anjubao.parking.municipal.parkinglot.controller;

import com.anjubao.parking.municipal.common.dto.ApiCode;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotDeviceChargingPileDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotDeviceChargingPileReturnDto;
import com.anjubao.parking.municipal.parkinglot.feign.ParkingLotFeignClient;
import com.anjubao.parking.municipal.parkinglot.form.ChargingPileQueryForm;
import com.anjubao.parking.municipal.parkinglot.form.ParkingLotDeviceChargingPileAddForm;
import com.anjubao.parking.municipal.parkinglot.model.ParkingLotDeviceChargingPile;
import com.anjubao.parking.municipal.parkinglot.service.ParkingLotDeviceChargingPileService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 充电桩API
 *
 * @author zhu.chunfeng
 */
@RestController
@RequestMapping("/parking-lots-device-charging-piles")
@Api(value = "ParkingLotDeviceChargingPileController", tags = {"充电桩管理API"})
@Slf4j
public class ParkingLotDeviceChargingPileController {

    @Resource
    private ParkingLotDeviceChargingPileService parkingLotDeviceChargingPileService;
    @Autowired
    private ParkingLotFeignClient parkingLotFeignClient;




    /**新增充电桩*/
    @ApiOperation(value = "2.2.1新增充电桩")
    @ApiImplicitParams({
            @ApiImplicitParam(name="form",required = true,dataType = "ParkingLotDeviceChargingPileAddForm",paramType = "body")
    })
    @PostMapping("")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<ParkingLotDeviceChargingPileReturnDto> createParkingLotDeviceChargingPile(@RequestBody @Validated ParkingLotDeviceChargingPileAddForm form, BindingResult result) {
        if(result.hasErrors()){
            List<ObjectError> ls=result.getAllErrors();
            return new ApiResult<>(ApiCode.STATUS_INTERNAL_ERROR.getCode(), String.valueOf(ls.get(0)));
        }

        ParkingLotDeviceChargingPile parkingLotDeviceChargingPile = parkingLotDeviceChargingPileService.createParkingLotDeviceChargingPileService(form);
        ParkingLotDeviceChargingPileReturnDto parkingLotBillRuleReturnCodeDto = new ParkingLotDeviceChargingPileReturnDto();
        parkingLotBillRuleReturnCodeDto.setParkingLotDeviceChargingPileCode(parkingLotDeviceChargingPile.getCode());
        return ApiResult.success(parkingLotBillRuleReturnCodeDto);
    }

    /**
     * 更改充电桩
     */
    @ApiOperation("2.2.2更改充电桩")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parkingLotDeviceChargingPileCode", value = "充电桩code", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "form", required = true, dataType = "ParkingLotDeviceChargingPileAddForm", paramType = "body")
    })
    @PutMapping("/{parkingLotDeviceChargingPileCode}")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<ParkingLotDeviceChargingPileReturnDto> updateParkingLotDeviceChargingPile(@PathVariable String parkingLotDeviceChargingPileCode, @RequestBody @Validated ParkingLotDeviceChargingPileAddForm form, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> ls = result.getAllErrors();
            return new ApiResult<>(ApiCode.STATUS_INTERNAL_ERROR.getCode(), String.valueOf(ls.get(0)));
        }

        parkingLotDeviceChargingPileService.updateParkingLotDeviceChargingPile(parkingLotDeviceChargingPileCode, form);
        ParkingLotDeviceChargingPileReturnDto parkingLotDeviceChargingPileDto = new ParkingLotDeviceChargingPileReturnDto();
        parkingLotDeviceChargingPileDto.setParkingLotDeviceChargingPileCode(parkingLotDeviceChargingPileCode);
        return ApiResult.success(parkingLotDeviceChargingPileDto);
    }

    /**查询充电桩*/
    @ApiOperation("2.2.3查询充电桩详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parkingLotDeviceChargingPileCode", value = "充电桩code", required = true, dataType = "string", paramType = "path")
    })
    @GetMapping("/{parkingLotDeviceChargingPileCode}")
    public ApiResult<ParkingLotDeviceChargingPileDto> getParkingLotDeviceChargingPile(@PathVariable String parkingLotDeviceChargingPileCode) {
        return ApiResult.success(parkingLotDeviceChargingPileService.getParkingLotDeviceChargePile(parkingLotDeviceChargingPileCode));
    }

    /**
     * 查询充电桩列表
     */
    @ApiOperation("2.2.4查询充电桩列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "int", paramType = "form")
    })
    @GetMapping("")
    public ApiResult<List<ParkingLotDeviceChargingPileDto>> getParkingLotDeviceChargingPile(ChargingPileQueryForm query, Integer page, Integer pageSize) {
        IPage<ParkingLotDeviceChargingPileDto> pileIPage = parkingLotDeviceChargingPileService.getParkingLotDeviceChargePilePages(query, page, pageSize);
        return ApiResult.success(pileIPage.getRecords(), (int) pileIPage.getTotal());
    }

    /**删除充电桩*/
    @ApiOperation("2.2.5删除充电桩")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parkingLotDeviceChargingPileCode", value = "充电桩code", required = true, dataType = "string", paramType = "path")
    })
    @DeleteMapping("/{parkingLotDeviceChargingPileCode}")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult deleteParkingLotDeviceChargingPile(@PathVariable String parkingLotDeviceChargingPileCode) {
        parkingLotDeviceChargingPileService.deleteParkingLotDeviceChargePile(parkingLotDeviceChargingPileCode);
        return ApiResult.success();
    }

    /**
     * 上线/下线 充电桩
     */
    @ApiOperation(value = "2.2.6上线/下线充电桩", notes = "上线/下线充电桩")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isOnline", value = "运行状态 0:断线 1:在线", required = true, dataType = "int"),
            @ApiImplicitParam(name = "parkingLotDeviceChargingPileCode", value = "设备编号", required = true, dataType = "string")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @PutMapping("/{parkingLotDeviceChargingPileCode}/change-online")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<ParkingLotDeviceChargingPileReturnDto> updateChargingPileOnline(@PathVariable String parkingLotDeviceChargingPileCode, @RequestParam Integer isOnline) {
        parkingLotDeviceChargingPileService.updateIsOnline(parkingLotDeviceChargingPileCode, isOnline);
        return ApiResult.success(new ParkingLotDeviceChargingPileReturnDto(parkingLotDeviceChargingPileCode));
    }


}
